package com.xing.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xing.reggie.common.BaseContext;
import com.xing.reggie.common.R;
import com.xing.reggie.dto.OrdersDto;
import com.xing.reggie.entity.OrderDetail;
import com.xing.reggie.entity.Orders;
import com.xing.reggie.entity.User;
import com.xing.reggie.service.OrderDetailService;
import com.xing.reggie.service.OrderService;
import com.xing.reggie.service.UserService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单管理
 */
@RestController
@Slf4j
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private UserService userService;

    /**
     * 订单分页查询
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, Long number, String beginTime, String endTime){

        log.info("id->{},beginTime->{},endTime->{}",number,beginTime,endTime);

        //分页构造器
        Page<Orders> ordersPage = new Page<>(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        queryWrapper.eq(number != null,Orders::getNumber,number);
        queryWrapper.gt(beginTime != null,Orders::getOrderTime,beginTime);
        queryWrapper.lt(endTime != null,Orders::getOrderTime,endTime);
        queryWrapper.orderByAsc(Orders::getOrderTime);
        //根据条件查询
        orderService.page(ordersPage,queryWrapper);
        return R.success(ordersPage);

    }

    @GetMapping("/userPage")
    public R<Page> userPage(int page,int pageSize){
        //分页构造器
        Page<Orders> ordersPage = new Page<>(page,pageSize);
        Page<OrdersDto> ordersDtoPage = new Page<>();
        //得到登录用户信息
        Long currentId = BaseContext.getCurrentId();
        User user = userService.getById(currentId);
        String phone = user.getPhone();
        //条件构造器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        queryWrapper.eq(Orders::getPhone, phone);
        queryWrapper.orderByDesc(Orders::getOrderTime);
        //查询
        orderService.page(ordersPage,queryWrapper);
        //拷贝
        BeanUtils.copyProperties(ordersPage,ordersDtoPage,"records");
        //通过手机号得到登录用户的订单信息
        List<Orders> ordersList = ordersPage.getRecords();
        List<OrdersDto> ordersDtoList = ordersList.stream().map((item)->{
            OrdersDto ordersDto = new OrdersDto();
            //拷贝信息
            BeanUtils.copyProperties(item,ordersDto);
            Long id = item.getId(); //订单号
            LambdaQueryWrapper<OrderDetail> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(OrderDetail::getOrderId,id);
            //得到订单对应详细信息
            List<OrderDetail> list = orderDetailService.list(wrapper);
            ordersDto.setOrderDetails(list);

            return ordersDto;

        }).collect(Collectors.toList());
        //设置记录值
        ordersDtoPage.setRecords(ordersDtoList);
        return R.success(ordersDtoPage);
    }

    /**
     * 订单提交处理
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String > submit(@RequestBody Orders orders){
        log.info("订单信息：{}",orders);
        orderService.submit(orders);
        return R.success("下单成功！");

    }

    /**
     * 根据提交状态值进行修改
     * @param orders
     * @return
     */
    @PutMapping
    public R<String> updateStatus(@RequestBody Orders orders){
        //条件构造器
        LambdaUpdateWrapper<Orders> updateWrapper = new LambdaUpdateWrapper<>();
        //添加条件
        updateWrapper.eq(orders.getId() != null,Orders::getId,orders.getId());
        updateWrapper.set(orders.getStatus() != null,Orders::getStatus,orders.getStatus());
        //根据条件查询
        orderService.update(updateWrapper);
        return R.success("修改成功!");
    }

}
